﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using GoodMan.Infrastructure.Entities.Entity;
using GoodMan.Infrastructure.Entities;

namespace GoodMan.Portal.Biz
{
    public class BusinessBiz
    {
        /// <summary>
        /// 模糊查询相关
        /// </summary>
        /// <param name="page"></param>
        /// <param name="takeSize"></param>
        /// <param name="search"></param>
        /// <returns></returns>
        public static ItemListBox<Business> GetAllBusinessByWhere(int page, int takeSize, string search = "", string style = "workspace")
        {
            using (Context ctx = new Context())
            {
                var query = from c in ctx.Business
                            select c;

                if (!string.IsNullOrEmpty(search))//商户名和联系方式
                {
                    query = query.Where(c => c.Name.Contains(search)||c.ContactInfo.Contains(search));
                }
                var data = query.OrderByDescending(c => c.CreateDate).Skip(page * takeSize).Take(takeSize).ToList().Select(c => new Business().Set(c)).ToList();
                var itemCount = query.Count();
                return new ItemListBox<Business>(data).BuildPage(itemCount, page, takeSize, new PageParameter()
                {
                    Style = style
                });
            }
        }

        /// <summary>
        /// true:不存在 false:存在
        /// </summary>
        /// <param name="businessName"></param>
        /// <returns></returns>
        public static bool BusinessNotExists(string businessName)
        {
            if (!String.IsNullOrEmpty(businessName))
            {
                using (Context ctx = new Context())
                {
                    return !ctx.Business.Any(c => c.Name == businessName);
                }
            }
            return false;
        }
    }
}
